export default {
  namespace: "echart",

  state: {
    collapsed: false,
    dataArr: [],
    dataArr1: [],
    dataArr2: [],
    dataArr3: [],
    dataArr4: [],
    dataArr5: [],
    dataArr6: [],
    dataArr7: [],
    xData: [],
    data: [],
    start: 0,
    end: 15,
    maxCompress: 1,
    minCompress: 1,
    averageYarr: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // eslint-disable-line
      //   if(history.location.pathname.match(/\/EchartPage/i)){
      // }
      // if (history.location.pathname.match(/\/AverageY/i)) {

      //   dispatch({
      //     type: "averageY",
      //     payload: {

      //     }
      //   })

      // }
    }
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      // eslint-disable-line
      yield put({ type: "setState" });
    },
    *relevant({ payload }, { call, put, select }) {
      // eslint-disable-line
      let dataArr = yield select((state) => state.echart.dataArr);
      let dataArr1 = yield select((state) => state.echart.dataArr1);
      let dataArr2 = yield select((state) => state.echart.dataArr2);
      let dataArr3 = yield select((state) => state.echart.dataArr3);
      let dataArr4 = yield select((state) => state.echart.dataArr4);
      let dataArr5 = yield select((state) => state.echart.dataArr5);
      let dataArr6 = yield select((state) => state.echart.dataArr6);
      let dataArr7 = yield select((state) => state.echart.dataArr7);
      var t = 1;
      var Ex = 0;
      var Ex_ = 0;
      var E2x = 0;
      var Ey1= 0, Ey2= 0, Ey3= 0, Ey4= 0, Ey5= 0, Ey6= 0, Ey7= 0;
      var Ey1_= 0, Ey2_= 0, Ey3_= 0, Ey4_= 0, Ey5_= 0, Ey6_= 0, Ey7_= 0;
      var E2y1= 0, E2y2= 0, E2y3= 0, E2y4= 0, E2y5= 0, E2y6= 0, E2y7= 0;
      var Exy1= 0, Exy2= 0, Exy3= 0, Exy4= 0, Exy5= 0, Exy6= 0, Exy7= 0;
      var Cov1= 0, Cov2= 0, Cov3= 0, Cov4= 0, Cov5= 0, Cov6= 0, Cov7= 0;
      var rouArr = [];

      for (var i = 0; i < dataArr.length; i++) {
        Ex = dataArr[i] + Ex;
        Ex_ = (dataArr[i] * dataArr[i]) + Ex_;

        Ey1 = dataArr1[i] + Ey1;
        Ey2 = dataArr2[i] + Ey2;
        Ey3 = dataArr3[i] + Ey3;
        Ey4 = dataArr4[i] + Ey4;
        Ey5 = dataArr5[i] + Ey5;
        Ey6 = dataArr6[i] + Ey6;
        Ey7 = dataArr7[i] + Ey7;

        Ey1_ = (dataArr1[i] * dataArr1[i]) + Ey1_;
        Ey2_ = (dataArr2[i] * dataArr2[i]) + Ey2_;
        Ey3_ = (dataArr3[i] * dataArr3[i]) + Ey3_;
        Ey4_ = (dataArr4[i] * dataArr4[i]) + Ey4_;
        Ey5_ = (dataArr5[i] * dataArr5[i]) + Ey5_;
        Ey6_ = (dataArr6[i] * dataArr6[i]) + Ey6_;
        Ey7_ = (dataArr7[i] * dataArr7[i]) + Ey7_;

        Exy1 = (dataArr[i] * dataArr1[i]) + Exy1;
        Exy2 = (dataArr[i] * dataArr2[i]) + Exy2;
        Exy3 = (dataArr[i] * dataArr3[i]) + Exy3;
        Exy4 = (dataArr[i] * dataArr4[i]) + Exy4;
        Exy5 = (dataArr[i] * dataArr5[i]) + Exy5;
        Exy6 = (dataArr[i] * dataArr6[i]) + Exy6;
        Exy7 = (dataArr[i] * dataArr7[i]) + Exy7;
        t++;
      }

      Ex = Ex / t;
      Ex_ = Ex_ / t;
      E2x = Ex * Ex;

      Ey1 = Ey1 / t;
      Ey2 = Ey2 / t;
      Ey3 = Ey3 / t;
      Ey4 = Ey4 / t;
      Ey5 = Ey5 / t;
      Ey6 = Ey6 / t;
      Ey7 = Ey7 / t;


      Ey1_ = Ey1_ / t;
      Ey2_ = Ey2_ / t;
      Ey3_ = Ey3_ / t;
      Ey4_ = Ey4_ / t;
      Ey5_ = Ey5_ / t;
      Ey6_ = Ey6_ / t;
      Ey7_ = Ey7_ / t;

      E2y1 = Ey1_ * Ey1_;
      E2y2 = Ey2_ * Ey2_;
      E2y3 = Ey3_ * Ey3_;
      E2y4 = Ey4_ * Ey4_;
      E2y5 = Ey5_ * Ey5_;
      E2y6 = Ey6_ * Ey6_;
      E2y7 = Ey7_ * Ey7_;

      Exy1 = Exy1 / t;
      Exy2 = Exy2 / t;
      Exy3 = Exy3 / t;
      Exy4 = Exy4 / t;
      Exy5 = Exy5 / t;
      Exy6 = Exy6 / t;
      Exy7 = Exy7 / t;

      Cov1 = Exy1 - Ex * Ey1;
      Cov2 = Exy2 - Ex * Ey2;
      Cov3 = Exy3 - Ex * Ey3;
      Cov4 = Exy4 - Ex * Ey4;
      Cov5 = Exy5 - Ex * Ey5;
      Cov6 = Exy6 - Ex * Ey6;
      Cov7 = Exy7 - Ex * Ey7;

      var sita1 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey1_ - E2y1);
      var sita2 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey2_ - E2y2);
      var sita3 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey3_ - E2y3);
      var sita4 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey4_ - E2y4);
      var sita5 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey5_ - E2y5);
      var sita6 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey6_ - E2y6);
      var sita7 = Math.sqrt(Ex_ - E2x) * Math.sqrt(Ey7_ - E2y7);
      
      var rou1 = Cov1 / sita1;
      var rou2 = Cov2 / sita2;
      var rou3 = Cov3 / sita3;
      var rou4 = Cov4 / sita4;
      var rou5 = Cov5 / sita5;
      var rou6 = Cov6 / sita6;
      var rou7 = Cov7 / sita7;

      rouArr.push(rou1);
      rouArr.push(rou2);
      rouArr.push(rou3);
      rouArr.push(rou4);
      rouArr.push(rou5);
      rouArr.push(rou6);
      rouArr.push(rou7);
    

      yield put({
        type: "setState",
        payload: {
          rouArr: rouArr,
        }
      });
    },
    *averageY({ payload }, { call, put, select }) {
      // eslint-disable-line
      let dataArr = yield select((state) => state.echart.dataArr);
      let dataArr1 = yield select((state) => state.echart.dataArr1);
      let dataArr2 = yield select((state) => state.echart.dataArr2);
      let dataArr3 = yield select((state) => state.echart.dataArr3);
      let dataArr4 = yield select((state) => state.echart.dataArr4);
      let dataArr5 = yield select((state) => state.echart.dataArr5);
      let dataArr6 = yield select((state) => state.echart.dataArr6);
      let dataArr7 = yield select((state) => state.echart.dataArr7);

      var averageYarr = [];
      for (var i = 0; i < dataArr.length; i++) {
        var sum = dataArr[i] + dataArr1[i] + dataArr2[i] + dataArr3[i] + dataArr4[i] + dataArr5[i] + dataArr6[i] + dataArr7[i];
        var ave = sum / 8;
        averageYarr.push(ave);

      }
      console.log(averageYarr);



      yield put({
        type: "setState",
        payload: {
          averageYarr: averageYarr,
        }
      });
    },
    *jiequ({ }, { call, put, select }) {
      // eslint-disable-line
      let start = yield select((state) => state.echart.start);
      let end = yield select((state) => state.echart.end);
      let dataArr = yield select((state) => state.echart.dataArr);
      let dataArr1 = yield select((state) => state.echart.dataArr1);
      let dataArr2 = yield select((state) => state.echart.dataArr2);
      let dataArr3 = yield select((state) => state.echart.dataArr3);
      let dataArr4 = yield select((state) => state.echart.dataArr4);
      let dataArr5 = yield select((state) => state.echart.dataArr5);
      let dataArr6 = yield select((state) => state.echart.dataArr6);
      let dataArr7 = yield select((state) => state.echart.dataArr7);

      var newdataArr = dataArr.slice(start, end);
      var newdataArr1 = dataArr1.slice(start, end);
      var newdataArr2 = dataArr2.slice(start, end);
      var newdataArr3 = dataArr3.slice(start, end);
      var newdataArr4 = dataArr4.slice(start, end);
      var newdataArr5 = dataArr5.slice(start, end);
      var newdataArr6 = dataArr6.slice(start, end);
      var newdataArr7 = dataArr7.slice(start, end);
      yield put({
        type: "setState",
        payload: {
          newdataArr: newdataArr,
          newdataArr1: newdataArr1,
          newdataArr2: newdataArr2,
          newdataArr3: newdataArr3,
          newdataArr4: newdataArr4,
          newdataArr5: newdataArr5,
          newdataArr6: newdataArr6,
          newdataArr7: newdataArr7,
        }

      });
    },
    *Maxyasuo({ payload }, { call, put, select }) {
      let maxCompress = yield select((state) => state.echart.maxCompress);
      let dataArr = yield select((state) => state.echart.dataArr);
      let dataArr1 = yield select((state) => state.echart.dataArr1);
      let dataArr2 = yield select((state) => state.echart.dataArr2);
      let dataArr3 = yield select((state) => state.echart.dataArr3);
      let dataArr4 = yield select((state) => state.echart.dataArr4);
      let dataArr5 = yield select((state) => state.echart.dataArr5);
      let dataArr6 = yield select((state) => state.echart.dataArr6);
      let dataArr7 = yield select((state) => state.echart.dataArr7);


      var maxdataArr = [];
      var maxdataArr1 = [];
      var maxdataArr2 = [];
      var maxdataArr3 = [];
      var maxdataArr4 = [];
      var maxdataArr5 = [];
      var maxdataArr6 = [];
      var maxdataArr7 = [];
      var maxCompressArr = [];
      var maxCompressArr1 = [];
      var maxCompressArr2 = [];
      var maxCompressArr3 = [];
      var maxCompressArr4 = [];
      var maxCompressArr5 = [];
      var maxCompressArr6 = [];
      var maxCompressArr7 = [];
      // eslint-disable-line
      for (var i = 0; i < dataArr.length; i += maxCompress) {
        maxdataArr.push(dataArr.slice(i, i + maxCompress));
        maxdataArr1.push(dataArr1.slice(i, i + maxCompress));
        maxdataArr2.push(dataArr2.slice(i, i + maxCompress));
        maxdataArr3.push(dataArr3.slice(i, i + maxCompress));
        maxdataArr4.push(dataArr4.slice(i, i + maxCompress));
        maxdataArr5.push(dataArr5.slice(i, i + maxCompress));
        maxdataArr6.push(dataArr6.slice(i, i + maxCompress));
        maxdataArr7.push(dataArr7.slice(i, i + maxCompress));
      }

      for (var i = 0; i < maxdataArr.length; i++) {
        maxCompressArr.push(Math.max.apply(Math, maxdataArr[i]));
        maxCompressArr1.push(Math.max.apply(Math, maxdataArr1[i]));
        maxCompressArr2.push(Math.max.apply(Math, maxdataArr2[i]));
        maxCompressArr3.push(Math.max.apply(Math, maxdataArr3[i]));
        maxCompressArr4.push(Math.max.apply(Math, maxdataArr4[i]));
        maxCompressArr5.push(Math.max.apply(Math, maxdataArr5[i]));
        maxCompressArr6.push(Math.max.apply(Math, maxdataArr6[i]));
        maxCompressArr7.push(Math.max.apply(Math, maxdataArr7[i]));
      }

      yield put({
        type: "setState",
        payload: {
          maxCompressArr: maxCompressArr,
          maxCompressArr1: maxCompressArr1,
          maxCompressArr2: maxCompressArr2,
          maxCompressArr3: maxCompressArr3,
          maxCompressArr4: maxCompressArr4,
          maxCompressArr5: maxCompressArr5,
          maxCompressArr6: maxCompressArr6,
          maxCompressArr7: maxCompressArr7,

        }
      });
    },
    *Minyasuo({ }, { call, put, select }) {
      let minCompress = yield select((state) => state.echart.minCompress);
      let dataArr = yield select((state) => state.echart.dataArr);
      let dataArr1 = yield select((state) => state.echart.dataArr1);
      let dataArr2 = yield select((state) => state.echart.dataArr2);
      let dataArr3 = yield select((state) => state.echart.dataArr3);
      let dataArr4 = yield select((state) => state.echart.dataArr4);
      let dataArr5 = yield select((state) => state.echart.dataArr5);
      let dataArr6 = yield select((state) => state.echart.dataArr6);
      let dataArr7 = yield select((state) => state.echart.dataArr7);


      var mindataArr = [];
      var mindataArr1 = [];
      var mindataArr2 = [];
      var mindataArr3 = [];
      var mindataArr4 = [];
      var mindataArr5 = [];
      var mindataArr6 = [];
      var mindataArr7 = [];
      var minCompressArr = [];
      var minCompressArr1 = [];
      var minCompressArr2 = [];
      var minCompressArr3 = [];
      var minCompressArr4 = [];
      var minCompressArr5 = [];
      var minCompressArr6 = [];
      var minCompressArr7 = [];
      // eslint-disable-line
      for (var i = 0; i < dataArr.length; i += minCompress) {
        mindataArr.push(dataArr.slice(i, i + minCompress));
        mindataArr1.push(dataArr1.slice(i, i + minCompress));
        mindataArr2.push(dataArr2.slice(i, i + minCompress));
        mindataArr3.push(dataArr3.slice(i, i + minCompress));
        mindataArr4.push(dataArr4.slice(i, i + minCompress));
        mindataArr5.push(dataArr5.slice(i, i + minCompress));
        mindataArr6.push(dataArr6.slice(i, i + minCompress));
        mindataArr7.push(dataArr7.slice(i, i + minCompress));
      }

      for (var i = 0; i < mindataArr.length; i++) {
        minCompressArr.push(Math.min.apply(Math, mindataArr[i]));
        minCompressArr1.push(Math.min.apply(Math, mindataArr1[i]));
        minCompressArr2.push(Math.min.apply(Math, mindataArr2[i]));
        minCompressArr3.push(Math.min.apply(Math, mindataArr3[i]));
        minCompressArr4.push(Math.min.apply(Math, mindataArr4[i]));
        minCompressArr5.push(Math.min.apply(Math, mindataArr5[i]));
        minCompressArr6.push(Math.min.apply(Math, mindataArr6[i]));
        minCompressArr7.push(Math.min.apply(Math, mindataArr7[i]));
      }

      yield put({
        type: "setState",
        payload: {
          minCompressArr: minCompressArr,
          minCompressArr1: minCompressArr1,
          minCompressArr2: minCompressArr2,
          minCompressArr3: minCompressArr3,
          minCompressArr4: minCompressArr4,
          minCompressArr5: minCompressArr5,
          minCompressArr6: minCompressArr6,
          minCompressArr7: minCompressArr7,

        }
      });
    },

  },

  reducers: {
    setState(state, action) {
      return { ...state, ...action.payload };
    }
  }
};
